﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Urb.Weather.Core.UI;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Urb.Weather.Ticker.UI
{
    /// <summary>
    /// Interaction logic for MainView.xaml
    /// </summary>
    public partial class MainView : Window, IMainView
    {
        private IMainViewModel _model;
        
        public MainView()
        {
            InitializeComponent();

            MouseDown += (obj, args) => { if (args.LeftButton == MouseButtonState.Pressed) DragMove(); };
            Loaded += new RoutedEventHandler(MainView_Loaded);
            SizeChanged += new SizeChangedEventHandler(MainView_SizeChanged);
        }

        public IMainViewModel Model
        {
            get
            {
                return _model;
            }
            set
            {
                _model = value; DataContext = _model;
            }
        }

        private void MainView_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (Screen.AllScreens.Length <= 1)
            {
                this.Top = SystemParameters.WorkArea.Height - e.NewSize.Height - 10;
                this.Left = SystemParameters.WorkArea.Width - e.NewSize.Width - 10;
            }
            else
            {
                var lastScreen = Screen.AllScreens.Where(s => !s.Primary).Last();

                this.Top = lastScreen.Bounds.Y + lastScreen.Bounds.Height - e.NewSize.Height - 10;
                this.Left = lastScreen.Bounds.X + lastScreen.Bounds.Width - e.NewSize.Width - 10;
            }            
        }

        private void MainView_Loaded(object sender, RoutedEventArgs e)
        {
            WindowInteropHelper wndHelper = new WindowInteropHelper(this);

            int exStyle = (int)GetWindowLong(wndHelper.Handle, (int)GetWindowLongFields.GWL_EXSTYLE);

            exStyle |= (int)ExtendedWindowStyles.WS_EX_TOOLWINDOW;
            SetWindowLong(wndHelper.Handle, (int)GetWindowLongFields.GWL_EXSTYLE, (IntPtr)exStyle);
        }

        #region Window styles

        [Flags]
        public enum ExtendedWindowStyles
        {
            // ...
            WS_EX_TOOLWINDOW = 0x00000080,
            // ...
        }

        public enum GetWindowLongFields
        {
            // ...
            GWL_EXSTYLE = (-20),
            // ...
        }

        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);

        public static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
        {
            int error = 0;
            IntPtr result = IntPtr.Zero;
            // Win32 SetWindowLong doesn't clear error on success
            SetLastError(0);

            if (IntPtr.Size == 4)
            {
                // use SetWindowLong
                Int32 tempResult = IntSetWindowLong(hWnd, nIndex, IntPtrToInt32(dwNewLong));
                error = Marshal.GetLastWin32Error();
                result = new IntPtr(tempResult);
            }
            else
            {
                // use SetWindowLongPtr
                result = IntSetWindowLongPtr(hWnd, nIndex, dwNewLong);
                error = Marshal.GetLastWin32Error();
            }

            if ((result == IntPtr.Zero) && (error != 0))
            {
                throw new System.ComponentModel.Win32Exception(error);
            }

            return result;
        }

        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", SetLastError = true)]
        private static extern IntPtr IntSetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

        [DllImport("user32.dll", EntryPoint = "SetWindowLong", SetLastError = true)]
        private static extern Int32 IntSetWindowLong(IntPtr hWnd, int nIndex, Int32 dwNewLong);

        private static int IntPtrToInt32(IntPtr intPtr)
        {
            return unchecked((int)intPtr.ToInt64());
        }

        [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
        public static extern void SetLastError(int dwErrorCode);

        #endregion
    }
}
